package com.aqarat.managedBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityExistsException;

import com.aqarat.controller.AqaratController;
import com.aqarat.controller.AqaratControllerImpl;
import com.aqarat.entity.Customer;
import com.core.managedBean.BaseBean;

@ManagedBean
@ViewScoped
public class CustomerBean extends BaseBean {
	private static final long serialVersionUID = 1L;
	private Customer customer;
	private AqaratController aqaratController;
	private List<Customer> customerList;

	@PostConstruct
	public void loadData() {
		if (customer == null) {
			customer = new Customer();
		}
		if (aqaratController == null) {
			aqaratController = new AqaratControllerImpl();
		}
	}

	public void save() {
		try {
			aqaratController.saveCustomer(customer);
			super.addInfoMessage("Successfully Saved");
		} catch (EntityExistsException ex) {
			super.addErrorMessage("Customer already Found");
			ex.printStackTrace();
		}catch (Exception e) {
			super.addErrorMessage("An Error Occured");
			e.printStackTrace();
		}
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public AqaratController getAqaratController() {
		return aqaratController;
	}

	public void setAqaratController(AqaratController aqaratController) {
		this.aqaratController = aqaratController;
	}

	public List<Customer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Customer> customerList) {
		this.customerList = customerList;
	}

}
